package com.zytlifeservice.controller.pay;

import com.qianmi.open.api.ApiException;
import com.zytlifeservice.domain.Result;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.service.pay.AlipayService;
import com.zytlifeservice.service.pay.FlowRechargeOrderService;
import com.zytlifeservice.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 手机流量下单控制类
 * Created by Administrator on 2017/6/3.
 */
@RestController
@RequestMapping(value = "/flowRecharge")
public class FlowRechargeOrderController {
    @Autowired
    private FlowRechargeOrderService flowRechargeOrderService;//手机流量订单业务对象

    @Autowired
    AlipayService alipayService;//阿里服务对象

    //手机流量下单
    @PostMapping(value = "/payFlow")
    public Result preCreate(@RequestParam("mobileNo") String mobileNo,
                            @RequestParam("flow") String flow,
                            @RequestParam("rechargeAmount") String rechargeAmount,
                            @RequestParam("userId") Long userId) {
        try {
            String[] result = flowRechargeOrderService.createOrder(mobileNo, rechargeAmount, flow, userId);
            if ("success".equals(result[0])) {
                return ResultUtil.success(result[1], "预下单成功");
            } else {
                throw new UserException(result[1], 202);
            }

        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("预下单失败", 202);
        }
    }

    @PostMapping(value = "/wxPayFlow")
    public Result wxPreCreate(@RequestParam("mobileNo") String mobileNo,
                              @RequestParam("flow") String flow,
                              @RequestParam("rechargeAmount") String rechargeAmount,
                              @RequestParam("userId") Long userId,
                              @RequestParam("ip") String ip) {

        try {
            String[] result = flowRechargeOrderService.wxPreCreate(mobileNo, rechargeAmount, flow, userId, ip);
            if ("success".equals(result[0])) {
                return ResultUtil.success(result[1], "预下单成功");
            } else {
                throw new UserException(result[1], 202);
            }
        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("预下单失败", 202);
        }

    }

    @PostMapping(value = "/moblieAndFlow")
    public Result phoneSend(String mobile, String flow) throws Exception {
        return flowRechargeOrderService.flowSend(mobile, flow);
    }


    //根据手机号查询
    @PostMapping(value = "/queryPhoneInfo")
    public Result queryPhoneInfo(String mobile) {
        return ResultUtil.success(flowRechargeOrderService.queryPhoneInfo(mobile), "查询成功");
    }
}
